import { mongo } from "../mogoConfig"
/**
 * aggregate
 * 管道
 * 
 */

const run = async () => {
    const db = mongo.db("sample_mflix")
    const moiveTB = db.collection("movies")

    const result1 = await db.command({
        aggregate: "movies",
        pipeline: [
            { $match: { year: { $gte: 1914 } } }
        ],
        cursor: { batchSize: 2 }
    })
    console.log("结果1-----------------------------")
    console.log(JSON.stringify(result1))

    const result2 = await moiveTB.aggregate([
        {
            $match: { year: { $gte: 1914 } }
        }
    ]).limit(1)
    console.log("结果2-----------------------------")
    for await (const item of result2) {
        console.log(JSON.stringify(item))
    }



}



run()
    .catch((error) => {
        console.log(error)
    })
    .finally(() => {
        mongo.close()
        console.log("执行结束")
    })